package com.kazusa.myservlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/*
* cookie的到期时间
* 负整数： 表示只在浏览器内存中存活，关闭浏览器失效
* 正整数： 表示单位为秒，表示存活指定秒数
* 0：删除cookie
* */
@WebServlet("/cookieTime")
public class CookieTime extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*到期时间：负整数（默认值-1，表示只在浏览器内存中存活，关闭浏览器失效）*/
        Cookie cookie1=new Cookie("uname1","zhangsan");
        cookie1.setMaxAge(-1);//关闭浏览器失效
        response.addCookie(cookie1);

        /*到期时间：正整数（单位：秒）*/
        Cookie cookie2=new Cookie("uname2","lisi");
        cookie2.setMaxAge(30);
        response.addCookie(cookie2);

        /*到期时间0：立即失效，表示删除cookie*/
        Cookie cookie3=new Cookie("uname3","wangwu");
        cookie3.setMaxAge(0);
        response.addCookie(cookie3);
    }
}
